package com.example.snowli01;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.snowli01.adapter.PageAdapter;
import com.example.snowli01.fragments.InsertFragment;
import com.example.snowli01.fragments.SelectFragment;
import com.example.snowli01.fragments.WordsFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout tab1;
    private ViewPager2 vp2;
    private List<Fragment> fragments;
    private PageAdapter pageAdapter;

    public static SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取控件对象
        getViews();
        //初始化子页面数据资源
        initPages();
        //初始化子页面适配器对象
        pageAdapter = new PageAdapter(fragments,this);
        //设置选项卡滑动方向
        vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //给viewPage2绑定适配器
        vp2.setAdapter(pageAdapter);
        //关联vp2和tablayout
        TabLayoutMediator mediator = new TabLayoutMediator(
                tab1,
                vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        //选项卡文本
                        switch (position){
                            case 0:
                                tab.setText("录入");
                                break;
                            case 1:
                                tab.setText("查询");
                                break;
                            case 2:
                                tab.setText("单词本");
                                break;
                        }
                    }
                }
        );
        //关联
        mediator.attach();


        //连接数据库
        //打开数据库，如果不存在则创建
        openDatabase();


    }


    private void openDatabase() {
        //数据库路径
        String path = getFilesDir().getAbsolutePath() + "/snowLiWork01.db";
        db = SQLiteDatabase.openOrCreateDatabase(path, null);
        //创建数据表
        String table = "create table if not exists words(" +
                "id integer primary key autoincrement," +
                "word varchar(20) not null," +
                "tran varchar(50) not null)";
        //建表
        //如果有就不重新建表了
        db.execSQL(table);


    }



    private void initPages() {
        fragments = new ArrayList<>();
        fragments.add(new InsertFragment());
        fragments.add(new SelectFragment());
        fragments.add(new WordsFragment());
    }

    private void getViews() {
        //获取控件对象
        tab1 = findViewById(R.id.tab1);
        vp2 = findViewById(R.id.vp2);

    }
}